package edu.gatech.cs2340.sharks.fms;

/**
 * AdminHomeActivity.java
 * @author Wai T. Yeung
 * 
 * Main screen for admins using the app. Shows a list of all users and their account status. 
 */

import android.app.Activity;
import edu.gatech.cs2340.sharks.fms.data.*;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import java.util.List;

public class AdminHomeActivity extends ListActivity
{
	/**
	 * The current instance of the activity, used to make sure there is only one instance of this activity running at a time.
	 */
	private static Activity instance = null;
	
   	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		if (instance != null) instance.finish();
		instance = this;
		
		ListView userListView;
		User[] userList;
		UserAdapter userAdapter;
		Packet<List<User>> ul = DBMgr.allUsers();
		
		if(ul.status.code() == Status.Code.OK)
		{
			userList = new User[ul.value.size()];
			for (int i = 0; i < ul.value.size(); i++) userList[i] = ul.value.get(i);
			userListView = getListView();
			userAdapter = new UserAdapter(this, R.layout.listview_user_row, userList);
			View header = (View)getLayoutInflater().inflate(R.layout.listview_user_header, null);
			userListView.addHeaderView(header, null, false);
			userListView.setClickable(true);
			userListView.setSelectionAfterHeaderView();
			setListAdapter(userAdapter);
		}
		else
		{
			Info.alert(this, ul.status.toString());
		}
	}

  /**
   * Method called when the "View User" button is clicked. Creates and passes an intent to create the ViewUserActivity, passing along the userID as an extra.
   * 
   * @param view a reference to the widget that was clicked
   */
	void viewUser(View view)
	{
		String userID = (String) view.getTag();
		Intent toViewUser = new Intent(this, ViewUserActivity.class);
		toViewUser.putExtra("userID", userID);
		startActivity(toViewUser);
	}
	
  /**
   * Method called when the "Add Admin" button is clicked. Creates and passes an intent to create the AddAdminActivity. 
   * 
   * @param view a reference to the widget that was clicked
   */
	public void addAdmin(View view)
	{
		startActivity(new Intent(this, AddAdminActivity.class));
	}

  /**
   * Method called when the "Log Out" button is clicked. Calls the Database Manager's Logout function and ends the AdminHomeActivity.
   * 
   * @param view a reference to the widget that was clicked
   */
	public void logout(View view)
	{
		DBMgr.logout(DBMgr.user); 
		finish(); 
	}

  /**
   * Method called when the "Manage Tags" button is clicked. Creates and passes an intent to create the ManageTagsActivity.
   * 
   * @param view a reference to the widget that was clicked
   */
	public void manageTags(View view)
	{
		startActivity(new Intent(this, ManageTagsActivity.class));
	}
}